/**
 * 更改密码页面
 * author wim
 */
import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter_aygsh/http/Api.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import '../../base/Constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../util/SignUtil.dart';
import '../../http/HttpUtil.dart';
import 'package:flutter_aygsh/base/Constant.dart';
import 'package:fluttertoast/fluttertoast.dart';

class ChangePwdView extends StatefulWidget {
  ChangePwdView({Key key}) : super(key: key);

  @override
  _ChangePwdViewState createState() => _ChangePwdViewState();
}

TextEditingController oldController = new TextEditingController();
TextEditingController new1Controller = new TextEditingController();
TextEditingController new2Controller = new TextEditingController();

class _ChangePwdViewState extends State<ChangePwdView> {
  String token = '';
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "修改密码",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.2,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      resizeToAvoidBottomPadding: false, //输入框抵住键盘
      body: new Container(
        color: divideColor,
        child: new Column(
          children: <Widget>[
            new Container(
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20)),
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5.0)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('原密码:',
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      controller: oldController,
                      maxLines: 1,
                       obscureText: true,
                      cursorColor: mainColor,
                      keyboardAppearance: Brightness.light,
                      decoration: new InputDecoration(
                          hintText: "",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  5,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20)),
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5.0)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text(
                      '新密码:',
                      style: TextStyle(
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                    ),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      controller: new1Controller,
                      maxLines: 1,
                      cursorColor: mainColor,
                       obscureText: true,
                      keyboardAppearance: Brightness.light,
                      decoration: new InputDecoration(
                          hintText: "",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_10),
                  ScreenUtil.getInstance().setHeight(DP_20),
                  ScreenUtil.getInstance().setHeight(DP_20)),
              padding: EdgeInsets.fromLTRB(
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0,
                  ScreenUtil.getInstance().setHeight(DP_20),
                  0),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5.0)),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('重复密码:',
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                  ),
                  Expanded(
                    flex: 3,
                    child: new TextField(
                      controller: new2Controller,
                      maxLines: 1,
                      obscureText: true,
                      keyboardAppearance: Brightness.light,
                      cursorColor: mainColor,
                      decoration: new InputDecoration(
                          hintText: "",
                          border: InputBorder.none,
                          hintStyle: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                  )
                ],
              ),
            ),
            new Container(
              width: double.infinity,
              height: ScreenUtil.getInstance().setHeight(DP_80),
              margin: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_80)),
              decoration: new BoxDecoration(
                  color: Color.fromARGB(255, 255, 140, 16),
                  borderRadius: BorderRadius.circular(
                      ScreenUtil.getInstance().setHeight(DP_80))),
              padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
              child: new FlatButton(
                child: new Padding(
                  padding: EdgeInsets.all(0.0),
                  child: new Text(
                    "确认修改",
                    style: new TextStyle(
                        color: Colors.white,
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  ),
                ),
                onPressed: () {
change();
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  change() async {
    Map map = new Map();
    map['token'] = token;
    map['sign_type'] = createSignType(Api.URL_USER_CHANGEPWD);
    map['oldpwd'] = oldController.text;
    map['pwd1'] = new1Controller.text;
    map['pwd2'] = new2Controller.text;
    Response<String> response =
        await HttpUtil().post(Api.URL_USER_CHANGEPWD, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Fluttertoast.showToast(msg: msg);
    if (code == 1) {
      Navigator.of(context).pop();
    }
  }
}
